<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Config,Request,Cache,App
};
class Useraddress extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['keyword']!=''){
                $map[] = ['u.username|u.phone','like',"%{$d['keyword']}%"];
            }
            $rs = model('UserAddress')->alias('ua')
                            ->leftJoin('user u','ua.user_id=u.id')
                            ->field('ua.*,u.username,u.phone as uphone,u.avatar')
                            ->where($map)
                            ->order('ua.id desc')
                            ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }

        return $this->fetch('');
    }

    
    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            if(model('UserAddress')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('UserAddress')->getError());
            }
        }
        $info = model('UserAddress')::get($id);
        return $this->fetch('',['info'=>$info]);
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
